package com.sl.au.controller;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.FileUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import com.alibaba.fastjson.JSON;


@RestController
@RequestMapping(value = "/ui/download") 
public class DownloadController extends BaseController {
	
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView index(ModelAndView modelAndView){
		modelAndView.setViewName("download/index");
		return modelAndView;
	}

	@RequestMapping(value = "downapk", method = RequestMethod.GET)
	public String downloadFile(HttpServletRequest request, HttpServletResponse response) {
		System.out.println("this is download!");
		
		Map<String, Object>map = getVersionJson();
		String version = (String) map.get("versionName");
	    String fileName = "Q-touch-V"+version+".apk";// 设置文件名，根据业务需要替换成要下载的文件名
	    if (fileName != null) {
	        //设置文件路径
	        String realPath = this.fileUploadPath+"APP/Android/";
	        File file = new File(realPath , fileName);
	        if (file.exists()) {
	            response.setContentType("application/force-download");// 设置强制下载不打开
	            response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名
	            byte[] buffer = new byte[1024];
	            FileInputStream fis = null;
	            BufferedInputStream bis = null;
	            try {
	                fis = new FileInputStream(file);
	                bis = new BufferedInputStream(fis);
	                OutputStream os = response.getOutputStream();
	                int i = bis.read(buffer);
	                while (i != -1) {
	                    os.write(buffer, 0, i);
	                    i = bis.read(buffer);
	                }
	                System.out.println("success");
	            } catch (Exception e) {
	                e.printStackTrace();
	            } finally {
	                if (bis != null) {
	                    try {
	                        bis.close();
	                    } catch (IOException e) {
	                        e.printStackTrace();
	                    }
	                }
	                if (fis != null) {
	                    try {
	                        fis.close();
	                    } catch (IOException e) {
	                        e.printStackTrace();
	                    }
	                }
	            }
	        }
	    }
	    return null;
	}
	
	@RequestMapping(value = "getVersion", method ={RequestMethod.POST,RequestMethod.GET})
	public Map<String, Object> getVersion(HttpServletRequest request, HttpServletResponse response) {
		Map<String, Object> map = new HashMap<String, Object>();

		map = getVersionJson();
		return map;
	}
	
	public Map<String, Object> getVersionJson(){
		Map<String, Object> map = new HashMap<String, Object>();
		String url = "http://" + request.getServerName() //服务器地址  
        + ":" + request.getServerPort()           //端口号  
        + request.getContextPath()      //项目名称  
        + "/ui/download/downapk";//请求页面或其他地址  
		//map.put("downloadUrl", url);
		String realPath = this.fileUploadPath+"APP/Android/versioninfo.json";
        try {
            File file = new File(realPath);
            if (file.exists()) {
            		String str = FileUtils.readFileToString(file, "UTF-8");
            		map = JSON.parseObject(str);
            		map.put("success", "true");
            }else {
            		map.put("success", "false");
            		map.put("versionName", "");
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        map.put("downloadUrl", url);
		return map;
	}
	
}
